package com.xcf.interceptor;

import cn.com.xcf.spring.AppContextConfig;
import com.xcf.entity.system.User;
import com.xcf.util.Const;
import com.xcf.util.Jurisdiction;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
* 类名称：登录过滤，权限验证
* 类描述： 
* @author 
* 作者单位： 
* 联系方式：
* 创建时间：2017年09月25日
* @version 1.6
 */
public class LoginHandlerInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String path = request.getServletPath();
		ServletContext servletContext = request.getSession().getServletContext();
		servletContext.setAttribute("ctx", request.getContextPath());
		if(AppContextConfig.getBoolean("staticFile.version.random",false)) {
			servletContext.setAttribute("staticFileVer",
					AppContextConfig.get("staticFile.version", "version=1.0"));
		}else {
			servletContext.setAttribute("staticFileVer",
					AppContextConfig.get("staticFile.version", "version="+System.currentTimeMillis()));
		}
		if(path.matches(Const.NO_INTERCEPTOR_PATH)){
			return true;
		}else{
			User user = (User)Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
			if(user!=null){
				path = path.substring(1, path.length());
				boolean b = Jurisdiction.hasJurisdiction(path); //访问权限校验
				if(!b){
					response.sendRedirect(request.getContextPath() + Const.LOGIN);
				}
				return b;
			}else{
				//登陆过滤
				response.sendRedirect(request.getContextPath() + Const.LOGIN);
				return false;		
			}
		}
	}
	
}
